<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.processDefName')">
              <a-input placeholder="" v-model="queryParam.processDefName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.taskTitle')">
              <a-input placeholder="" v-model="queryParam.taskTitle"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.taskType')">
              <a-select v-model="queryParam.taskType" placeholder="">
                <a-select-option value="all">{{this.$t('m.common.label.all')}}</a-select-option>
                <a-select-option value="1">{{$t('m.bpm.other.option_todoed')}}</a-select-option>
                <a-select-option value="2">{{$t('m.bpm.other.option_passed')}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">

          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? this.$t('m.common.button.fold') : this.$t('m.common.button.expand') }}
                <ych-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('m.common.button.open') }}</a>
          <!--<a-divider type="vertical"/>
          <a @click="handleDetail2(record)">打开2</a>
          <a-divider type="vertical"/>
          <a @click="handleDetail3(record)">打开3</a>-->
        </span>

      </a-table>
    </div>

    <redirect-modal ref="flowForm" @refreshParent="modalFormOk"></redirect-modal>
  </a-card>
</template>

<script>

  import {YcListMixin} from '@/mixins/YcListMixin';
  import RedirectModal from './redirect/RedirectModal';
  import YchConstant from "@/api/YchConstant";

  export default {
    name: "HistTaskList",
    mixins: [YcListMixin],
    components: {
      RedirectModal,
    },
    data() {
      return {
        // 表头
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: "50px",
            align: "center",
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.bpm.entity.taskTitle'),
            align: 'left',
            dataIndex: 'taskTitle',
            ellipsis: true,
          },
          {
            title: this.$t('m.bpm.entity.processDefName'),
            align: 'left',
            width: '150px',
            dataIndex: 'processDefName',
            ellipsis: true,
          },
          /*{
            title: '优先级',
            align: "center",
            dataIndex: 'priority',
            width: 80
          },*/
          {
            title: this.$t('m.bpm.entity.taskType'),
            align: "center",
            dataIndex: 'identityString',
            width: "90px"
          },
          {
            title: this.$t('m.bpm.entity.fromUserName'),
            align: "center",
            dataIndex: 'fromUserName',
            width: "150px",
            customRender: function (t, r, index) {
              return t + (r.fromDeptName ? '/' + r.fromDeptName : '');
            }
          },
          {
            title: this.$t('m.bpm.entity.sendTimeString'),
            align: "center",
            dataIndex: 'sendTimeString',
            width: "130px"
          },
          {
            title: this.$t('m.bpm.entity.endTimeString'),
            align: "center",
            dataIndex: 'endTimeString',
            width: "130px"
          },
          {
            title: this.$t('m.bpm.entity.dueDateString'),
            align: "center",
            dataIndex: 'dueDateString',
            width: "130px"
          },
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: "center",
            scopedSlots: {customRender: 'action'},
            width: "90px"
          }
        ],
        url: {
          list: YchConstant.PREFIX_BPM + "/bpm-front/histTaskRest/list/v1"
        }
      }
    },
    inject: ['addRouter'],
    methods: {
      handleDetail(record) {
        if (record.pcUrl.startsWith("router:")) {
          let url = record.pcUrl.substr("router:".length);
          if(url.indexOf("?") === -1){
            url += "?taskId=" + record.id + "&finish=1"
          } else {
            url += "&taskId=" + record.id + "&finish=1"
          }
          this.$router.push(url).catch(err => {})
          return
        } else if (record.pcUrl.startsWith("http:") || record.pcUrl.startsWith("https:")) {
          // http 开头的链接，暂时以新页签形式打开
          let url = record.pcUrl;
          if (url.indexOf("?") === -1) {
            url += "?taskId=" + record.id + "&finish=1"
          } else {
            url += "&taskId=" + record.id + "&finish=1"
          }
          window.open(url)
          return
        }
        let router = null
        if (this.$store.getters.formOpenType === '2') {
          router = this.addRouter({
            path: '/bpm/detailFromTask/' + record.id,
            component: resolve => require(['@/views/bpm/redirect/RedirectTab.vue'], resolve),
            props: {
              id: record.id,
              func: 'detailFromTask',
            },
            meta: {
              title: this.$t('m.bpm.common.bpmDetail'),
            }
          });
          if (router) {
            this.$router.push(router).catch(err => {})
          }
        }
        if (!router) {
          this.$refs.flowForm.detailFromTask(record.id)
        }
      },
    }
  }
</script>
<style lang="less" scoped>
</style>
